﻿using EquipmentManagement.Application.IServices;
using EquipmentManagement.Domain.Entities;
using Infrastructure;
using Standard;
using Standard.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace EquipmentManagement.Application.Services
{
    public class ToolingFixturesService : IToolingFixturesService
    {
        private readonly MesRepository<ToolingFixtures> _mesRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mesRepository"></param>
        public ToolingFixturesService(MesRepository<ToolingFixtures> mesRepository)
        {
            _mesRepository = mesRepository;
        }
        /// <summary>
        /// 添加具体实现
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ApiResult<int> Add(ToolingFixtures model)
        {
            try
            {
                model.ID = YitIdHelper.NextId().ToString();
                model.CreatedUserName = "admin";
                model.CreatedTime = DateTime.Now;
                model.IsDeleted = false;
                var i = _mesRepository.Add(model);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = i,
                    Message = "添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除(批量)具体实现
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> Delete(string id)
        {
            try
            {
                var ids = id.Split(",");
                var models = _mesRepository.GetQueryable(x => x.IsDeleted == false).ToList();
                var list = new List<ToolingFixtures>();
                foreach (var i in ids)
                {
                    list.Add(models.FirstOrDefault(x => x.ID == i)!);
                }
                list.ForEach(x => x.IsDeleted = true);
                _mesRepository.BatchUpdate(list);
                return new ApiResult<int>()
                {
                    Code = ApiCode.Success,
                    Message = "删除成功",
                    ResultData = 1
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取列表(分页,查询)具体实现
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<ToolingFixtures> Get(PagingInput paging, string? SerialNumber, string? TypeId, string? UnitId)
        {
            var model = _mesRepository.GetQueryable(x => x.IsDeleted == false);
            if (!string.IsNullOrEmpty(SerialNumber)) 
            {
                model=model.Where(x => x.SerialNumber.Contains(SerialNumber));
            }
            if (!string.IsNullOrEmpty(TypeId)) 
            {
                model=model.Where(x => x.TypeId==TypeId);
            }
            if (!string.IsNullOrEmpty(UnitId)) 
            {
                model=model.Where(x => x.UnitId==UnitId);
            }
            return new ApiPaging<ToolingFixtures>
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / paging.PageSize),
                PageDatas = model.OrderByDescending(x => x.CreatedTime).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList()
            };
        }
        /// <summary>
        /// 修改具体实现
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> Update(ToolingFixtures model)
        {
            try
            {
                model.UpdateTime = DateTime.Now;
                model.UpdateUserName = "admin";
                var i = _mesRepository.Update(model);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    Message = "修改成功",
                    ResultData = i
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
